package cn.xd.base.controller.middle;

import cn.xd.base.application.dto.query.KeyValueQuery;
import cn.xd.base.application.service.common.KeyValueAppService;
import cn.xd.base.application.service.query.KeyValueQueryAppService;
import cn.xd.base.basic.type.DictCode;
import cn.xd.base.basic.type.KeyValueId;
import cn.xd.base.domain.bean.command.AddKeyValueCommand;
import cn.xd.base.domain.bean.command.DeleteKeyValueCommand;
import cn.xd.base.domain.bean.command.UpdateKeyValueCommand;
import cn.xd.base.domain.bean.response.JsonKeyValueTree;
import cn.xd.base.domain.bean.response.KeyValueDTO;
import cn.xd.common.response.ResponseResult;
import cn.xd.web.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : mirzaLin
 * @data : 2023/11/20
 * @description : 字典交互层
 **/
@RestController
@RequestMapping("/dictKeyValue/")
public class DictKeyValueController {
    @Resource
    private KeyValueAppService keyValueAppService;
    @Resource
    private KeyValueQueryAppService keyValueQueryAppService;

    /**
     * 获取字典配置列表（Tree型）
     *
     * @param query
     * @return
     */
    @PostMapping("treeList")
    public ResponseResult<List<JsonKeyValueTree>> getTreeList(@Validated @RequestBody KeyValueQuery query) {
        return ResponseResult.success(keyValueQueryAppService.getKeyValueJsonTree(query.getFilterWithoutShow(), -1, DictCode.of(query.getDictCode()), KeyValueId.of(query.getPrentId())));
    }

    /**
     * 获取字典配置列表（Tree型）
     *
     * @param query
     * @return
     */
    @PostMapping("treeList/{maxLevel}")
    public ResponseResult<List<JsonKeyValueTree>> getTreeList(@Validated @RequestBody KeyValueQuery query, @PathVariable Integer maxLevel) {
        return ResponseResult.success(keyValueQueryAppService.getKeyValueJsonTree(query.getFilterWithoutShow(), maxLevel, DictCode.of(query.getDictCode()), KeyValueId.of(query.getPrentId())));
    }

    /**
     * 获取字典配置数据信息
     *
     * @param id
     * @return
     */
    @PostMapping("get/{id}")
    public ResponseResult<KeyValueDTO> getById(@PathVariable Long id) {
        return ResponseResult.success(keyValueQueryAppService.getById(id));
    }

    /**
     * 增加字典配置
     *
     * @param command 新增操作
     * @return 增加的字典配置
     */
    @PostMapping("add")
    public ResponseResult<?> add(@Validated @RequestBody AddKeyValueCommand command) {
        keyValueAppService.addKeyValue(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }


    /**
     * 修改字典配置
     *
     * @param command 修改操作
     * @return 修改后的字典配置
     */
    @PostMapping("update")
    public ResponseResult<?> update(@Validated @RequestBody UpdateKeyValueCommand command) {
        keyValueAppService.updateKeyValue(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 删除字典配置
     *
     * @param command 配置项 type value
     * @return 状态
     */
    @PostMapping("delete")
    public ResponseResult<?> delete(@Validated @RequestBody DeleteKeyValueCommand command) {
        keyValueAppService.deleteKeyValue(command);
        return ResponseResult.success();
    }


//    /**
//     * 增加字典配置（json批量导入）
//     *
//     * @param batchCommand json
//     * @return 状态
//     */
//    @PostMapping("add/json")
//    public ResponseResult<?> add(@Validated @RequestBody AddKeyValueBatchCommand batchCommand) {
//        // json处理
//        Map<String, JSONObject> keyValue = JSON.parseObject(batchCommand.getJson(), Map.class);
//        // 转化commandList
//        List<AddKeyValueCommand> commandList = new ArrayList<>();
//        for (String key : keyValue.keySet()) {
//            KeyValueValueMarkDTO keyValueValueMarkDTO = JSON.toJavaObject(keyValue.get(key), KeyValueValueMarkDTO.class);
//            if (ObjUtil.isNull(keyValueValueMarkDTO.getValue())) {
//                throw new BizException("name为" + key + "的value不存在，加入失败");
//            }
//            AddKeyValueCommand command = AddKeyValueCommand.builder().dictKey(keyValueValueMarkDTO.getValue()).dictValue(key).dictCode(batchCommand.getDictCode())
//                    .parentId(keyValueValueMarkDTO.getParentId()).mark(keyValueValueMarkDTO.getMark()).build();
//            commandList.add(command);
//        }
//        // 新增操作
//        for (AddKeyValueCommand command : commandList) {
//            keyValueAppService.addKeyValue(command, SecurityUtils.getUserId());
//        }
//        return ResponseResult.success();
//    }
}
